import java.util.Stack;

public class Leet515 {
      public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
    /*
    LCR 125. 图书整理 II
     */
    class CQueue {
        Stack<Integer> A;//进
        Stack<Integer> B;//出
        public CQueue() {
                A  = new Stack<>();
                B = new Stack<>();
        }
    //进元素就放进A
        public void appendTail(int value) {
            A.push(value);
        }
        //出队首的元素就把A的元素放进B，再出B的元素，就是队首的元素
        public int deleteHead() {
            if (!B.empty()){
                return B.pop();
            }else if (A.empty()){
                return -1;//两个栈都为空
            }else{
                while (!A.empty()){
                    B.push(A.pop());
                }
            }
            return B.pop();
        }
    }

/*
LCR 142. 训练计划 IV
 */
    public ListNode trainningPlan(ListNode l1, ListNode l2) {
            if (l1 == null) return l2;
            if (l2 ==null )return l1;
            ListNode yum = new ListNode(-1);
            ListNode cur = yum;
            while(l1 != null && l2 != null){
                if (l1.val < l2.val){
                    cur.next=l1;
                    l1 = l1.next;
                }else{
                    cur.next=l2;
                    l2 = l2.next;
                }
                cur = cur.next;
            }
            if (l1 != null){
                cur.next=l1;
            }else{
                cur.next =l2;
            }
            return yum.next;
    }

    /*
    LCR 140. 训练计划 II
     */
    public ListNode trainingPlan(ListNode head, int cnt) {
            ListNode fast = head;
            ListNode slow = head;
            int len = cnt;
            while (fast != null && len > 0){
                fast  = fast.next;
                len--;
            }
            while (fast != null){
                fast = fast.next;
                slow = slow.next;
            }
            return slow;
    }
}
